/**
 * @author Jason
 * sourc page string parameter needed
 */
package com.cats.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.cats.R;
import com.cats.adapter.AddressListAdapter;
import com.cats.model.AddressModel;

public class AddressListActivity extends BaseActivity {
	private ArrayList<AddressModel> addressList = null;
	private ListView listView = null;
	private AddressListAdapter adapter = null;
	private String sourcePageString = ""; // default empty string
	
	public AddressListActivity() {
		// TODO Auto-generated constructor stub
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		System.out.println("[AddressListActivity]onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_list);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		setActionBarTitle("Address List");
		
		Bundle b = getIntent().getExtras();
		if (b != null && b.getString("SOURCE") != null) {
			sourcePageString = b.getString("SOURCE");
			System.out.println("[ShipmentActivity]:onCreate() with SOURCE" + sourcePageString);
		}
		
		// Assume we have addresses ready
		addressList = new ArrayList<AddressModel>();
		addressList.add(new AddressModel("Mike1", "2801 S University Ave", "", "Little Rock", "AR", "72204", "United States", "5010000000", "testaddress@ualr.edu"));
		addressList.add(new AddressModel("Mike2", "2801 S University Ave", "", "Little Rock", "AR", "72204", "United States", "5010000000", "testaddress@ualr.edu"));
		addressList.add(new AddressModel("Mike3", "2801 S University Ave", "", "Little Rock", "AR", "72204", "United States", "5010000000", "testaddress@ualr.edu"));
		addressList.add(new AddressModel("Mike4", "2801 S University Ave", "", "Little Rock", "AR", "72204", "United States", "5010000000", "testaddress@ualr.edu"));
		addressList.add(new AddressModel("Mike5", "2801 S University Ave", "", "Little Rock", "AR", "72204", "United States", "5010000000", "testaddress@ualr.edu"));
		// TODO:request address list
		
		
		adapter = new AddressListAdapter(this, R.layout.addresses_listview_item, addressList);
		listView = (ListView)findViewById(R.id.addressListView);
		listView.setAdapter(adapter);	
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				int position = arg2;

				System.out.println("[MemberActivity]Select address " + position);
				
				if (sourcePageString.equalsIgnoreCase("com.cats.activity.PickupActivity")) {
					// go back to pickup, return selected address model as parameter
					Intent intent = new Intent();
					AddressModel addressModel = adapter.getItem(position);
					intent.putExtra("ADDRESS", addressModel);
					setResult(RESULT_OK, intent);
					finish();
				}
				else {
					// navigate to addresses page
					final Intent intent = new Intent(AddressListActivity.this, AddressDetailActivity.class);
					//TODO:parameter
					AddressModel addressModel = adapter.getItem(position);
					intent.putExtra("ADDRESS", addressModel);
				    startActivity(intent);					
				}
			}
		});
	}

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			if (sourcePageString.equalsIgnoreCase("com.cats.activity.PickupActivity")) {
				Intent parentActivityIntent;
				try {
					parentActivityIntent = new Intent(this, Class.forName(sourcePageString));
					parentActivityIntent.addFlags(
							Intent.FLAG_ACTIVITY_CLEAR_TOP |
			                Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(parentActivityIntent);
			        finish();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else 
				NavUtils.navigateUpFromSameTask(this);
			
			return true;
        case R.id.action_add:
        	Intent intent = new Intent(AddressListActivity.this, NewAddressActivity.class);
        	startActivity(intent);
            return true;
		}
		
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	/**
	 * init action bar
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
	    // Inflate the menu items for use in the action bar
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.addresses_activity_actions, menu);
	    return super.onCreateOptionsMenu(menu);
	}
}
